import 'package:flutter/material.dart';

// 有状态控件含有数据 关键字 StatefulWidget
class Music extends StatefulWidget {
  const Music({super.key});

  @override
  musicState createState() {
    return musicState();
  }
}

// 有状态控件必须结合一个状态管理类，来进行实现
// // ignore: camel_case_types
// class musicState extends State<Music> {
//   @override
//   Widget build(BuildContext context) {
//     return const Text("音乐页面");
//   }
// }

// class Music extends StatefulWidget {
//   @override
//   _MyHomePageState createState() => _MyHomePageState();
// }

class musicState extends State<Music> with SingleTickerProviderStateMixin {
  late ScrollController _scrollController;
  int _itemCount = 20;
  bool _loading = false;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
              _scrollController.position.maxScrollExtent &&
          !_loading) {
        setState(() {
          _loading = true;
        });
        // 模拟加载更多数据，这里可以使用Future.delayed来模拟异步操作
        Future.delayed(Duration(milliseconds: 10), () {
          setState(() {
            _itemCount += 20;
            _loading = false;
          });
        });
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      controller: _scrollController,
      itemCount: _itemCount,
      itemBuilder: (context, index) {
        return ListTile(title: Text('Item $index'));
      },
    );
  }
}
